package com.ruoyi.sys.service.impl;

import java.util.List;
import java.util.Map;
import java.util.ArrayList;

import cn.hutool.core.collection.CollUtil;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.enums.DataSourceType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.db.mapper.AddressInfoMapper;
import com.ruoyi.db.domain.AddressInfo;
import com.ruoyi.sys.service.IAddressInfoService;
import com.ruoyi.common.core.text.Convert;

/**
 * 地址管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-03-06
 */
@Service
@DataSource(DataSourceType.SLAVE)
public class AddressInfoServiceImpl implements IAddressInfoService 
{
    @Autowired
    private AddressInfoMapper addressInfoMapper;

    /**
     * 查询地址管理
     * 
     * @param codeValue 地址管理主键
     * @return 地址管理
     */
    @Override
    public AddressInfo selectAddressInfoByCodeValue(String codeValue)
    {
        return addressInfoMapper.selectAddressInfoByCodeValue(codeValue);
    }

    /**
     * 查询地址管理列表
     * 
     * @param addressInfo 地址管理
     * @return 地址管理
     */
    @Override
    public List<AddressInfo> selectAddressInfoList(AddressInfo addressInfo)
    {
        return addressInfoMapper.selectAddressInfoList(addressInfo);
    }

    /**
     * 查询地址管理列表
     *
     * @param addressInfo 地址管理
     * @return 地址管理
     */
    @Override
    public List<Map<String, Object>> selectAddressInfoListMap(AddressInfo addressInfo)
    {
        return addressInfoMapper.selectAddressInfoListMap(addressInfo);
    }

    /**
     * 新增地址管理
     * 
     * @param addressInfo 地址管理
     * @return 结果
     */
    @Override
    public int insertAddressInfo(AddressInfo addressInfo)
    {
        return addressInfoMapper.insertAddressInfo(addressInfo);
    }

    /**
     * 修改地址管理
     * 
     * @param addressInfo 地址管理
     * @return 结果
     */
    @Override
    public int updateAddressInfo(AddressInfo addressInfo)
    {
        return addressInfoMapper.updateAddressInfo(addressInfo);
    }

    /**
     * 批量删除地址管理
     * 
     * @param codeValues 需要删除的地址管理主键
     * @return 结果
     */
    @Override
    public int deleteAddressInfoByCodeValues(String codeValues)
    {
        return addressInfoMapper.deleteAddressInfoByCodeValues(Convert.toStrArray(codeValues));
    }

    /**
     * 删除地址管理信息
     * 
     * @param codeValue 地址管理主键
     * @return 结果
     */
    @Override
    public int deleteAddressInfoByCodeValue(String codeValue)
    {
        return addressInfoMapper.deleteAddressInfoByCodeValue(codeValue);
    }

    /**
     * 查询地址管理树列表
     * 
     * @return 所有地址管理信息
     */
    @Override
    public List<Ztree> selectAddressInfoTree()
    {
        List<AddressInfo> addressInfoList = addressInfoMapper.selectAddressInfoList(new AddressInfo());
        List<Ztree> ztrees = new ArrayList<Ztree>();
        for (AddressInfo addressInfo : addressInfoList)
        {
            Ztree ztree = new Ztree();
            ztree.setId(Long.valueOf(addressInfo.getCodeValue()));
            ztree.setpId(addressInfo.getParentId());
            ztree.setName(addressInfo.getName());
            ztree.setTitle(addressInfo.getName());
            ztrees.add(ztree);
        }
        return ztrees;
    }

    @Override
    public List<Ztree> selectAddressInfoTreeOfChoose(List<String> codeValueList)
    {
        List<AddressInfo> addressInfoList = addressInfoMapper.selectAddressInfoList(new AddressInfo());
        List<Ztree> ztrees = new ArrayList<Ztree>();
        for (AddressInfo addressInfo : addressInfoList)
        {
            Ztree ztree = new Ztree();
            ztree.setId(Long.valueOf(addressInfo.getCodeValue()));
            ztree.setpId(addressInfo.getParentId());
            ztree.setName(addressInfo.getName());
            ztree.setTitle(addressInfo.getName());

            if (CollUtil.isNotEmpty(codeValueList)){
                ztree.setChecked(codeValueList.contains(addressInfo.getCodeValue()));
            }

            ztrees.add(ztree);
        }
        return ztrees;
    }
}
